<template>
  <div>
    <!-- 新增 修改 -->
    <el-dialog
      width="500px"
      title="title"
      :title="type == 'add' ? $t('添加') : $t('编辑')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel">
      <el-form ref="elForm" :model="form" :rules="rules" label-width="90px" label-position="right">
        <!-- 租户 推荐人/分成 推荐人上级/分成 总公司/分成  -->

        <el-form-item :label="$t('分组名')" prop="groupName">
          <el-input v-model="form.groupName"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" plain @click="cancel">
          <!-- cancel -->
          {{ $t('取消') }}
        </el-button>
        <!-- ******************* 新增 ******************* -->
        <el-button v-if="type == 'add'" size="small" plain @click="handleAdd" type="primary">
          <!-- 确定 -->
          {{ $t('确定') }}
        </el-button>
        <!-- ******************* 新增 end ******************* -->

        <!-- ******************* 修改 ******************* -->
        <el-button v-if="type == 'edit'" size="small" plain @click="handleEdit" type="primary">
          <!-- 修改 -->
          {{ $t('编辑') }}
        </el-button>
        <!-- ******************* 修改 end ******************* -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {addCloudGroup, updateCloudGroup} from '@/api/platform/cloudInvisibleGroup'

  export default {
    activated() {
      this.$nextTick(() => this.$refs['TableRef']?.doLayout())
    },
    props: {
      /**
       * formData
       */
      formData: {
        type: Object,
        default: {},
      },
      /**
       * type
       */
      type: {
        type: String,
        required: true,
      },
      /**
       * 控制显示隐藏
       */
      show: {
        type: Boolean,
        required: true,
        default: false,
      },
    },
    data() {
      return {
        form: {
          groupName: '',
        },
        rules: {
          groupName: [{required: true, message: '请输入分组名称', trigger: 'blur'}],
        },
      }
    },
    computed: {},
    watch: {
      show: function (newValue) {
        if (newValue) {
          this.resetForm()
          if (this.type == 'edit') {
            Object.keys(this.formData).forEach((key) => {
              this.form[key] = this.formData[key]
            })
            console.log(`this.form ==>`, JSON.parse(JSON.stringify(this.form)))
          }
        }
      },
    },
    methods: {
      resetForm() {
        Object.keys(this.form).forEach((key) => (this.form[key] = ''))
      },

      handleAdd() {
        this.$refs['elForm'].validate((valid) => {
          if (valid) {
            addCloudGroup(this.form).then((res) => {
              const {code, data, msg} = res
              if (code === 200) {
                this.$message({type: 'success', message: msg})
                this.cancel()
                this.$emit('getList')
              }
            })
          }
        })
      },
      handleEdit() {
        this.$refs['elForm'].validate((valid) => {
          if (valid) {
            updateCloudGroup(this.form).then((res) => {
              const {code, data, msg} = res
              if (code === 200) {
                this.$message({type: 'success', message: msg})
                this.cancel()
                this.$emit('getList')
              }
            })
          }
        })
      },
      cancel() {
        this.$emit('update:show', false)
      },
    },
  }
</script>
<style scoped lang="scss"></style>
